//
//  MRMWallsViewController.m
//  MeiRiMoney
//
//  Created by qiuqk on 13-7-23.
//  Copyright (c) 2013年 MeiRiCai. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "MRMConfig.h"
#import "MRMSdk.h"
#import "MRMWallsViewController.h"
#import "MRMDianJoyAdWallDetegateInstance.h"
#import "MRMDianRuAdWallDetegateInstance.h"
#import "MRMDMOfferWallDelegate.h"
#import "MRMWebViewController.h"
#import "MRMData.h"
#import "YouMiWall.h"

#if TARGET_IPHONE_SIMULATOR
NSString *Mobile_URL_Prefix = @"http://test.m.meiricai.com";
#else
NSString *Mobile_URL_Prefix = @"http://m.meiricai.com";
#endif

@interface MRMWallsViewController ()
{
	NSArray *_TableViewDataSource;
	
	//YouMiWall *_YouMiWall;
	//MRMYouMiWallDelegateInstance *_YouMiDelegate;
	
	DianJoyLandingPageView *_DianJoyView;
	MRMDianJoyAdWallDetegateInstance *_DianJoyDelegate;
	
	MRMDianRuAdWallDetegateInstance *_DianRuDelegate;
	
	DMOfferWallViewController *_DomobWallController;
	MRMDMOfferWallDelegate *_DomobDelegate;
}

@property (weak, nonatomic) IBOutlet UITableView *wallsTableView;

@end

@implementation MRMWallsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
	[self initTableViewDataSource];
	self.wallsTableView.dataSource = self;
	self.wallsTableView.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
	[self setWallsTableView:nil];
	[super viewDidUnload];
}

- (void)initTableViewDataSource
{
	BOOL isEnabledWall = [MRMData isEnabledWall];
	
	NSMutableArray *mArray = [[NSMutableArray alloc] init];
	if (isEnabledWall) {
		TaskItem *item_youmi = [[TaskItem alloc] initWithKey:@"youmi" Name:@"任务列表A" Serial:1 Category:1];
		[mArray addObject:item_youmi];
	}
	
	if (isEnabledWall) {
		TaskItem *item_dianjoy = [[TaskItem alloc] initWithKey:@"dianjoy" Name:@"任务列表B" Serial:2 Category:1];
		[mArray addObject:item_dianjoy];
	}
	if (isEnabledWall) {
		TaskItem *item_dianru = [[TaskItem alloc] initWithKey:@"dianru" Name:@"任务列表C" Serial:3 Category:1];
		[mArray addObject:item_dianru];
	}
	if (isEnabledWall) {
		TaskItem *item_domob = [[TaskItem alloc] initWithKey:@"domob" Name:@"任务列表D" Serial:4 Category:1];
		[mArray addObject:item_domob];
	}
	
	TaskItem *item_sign = [[TaskItem alloc] initWithKey:@"sign" Name:@"签到赚积分" Serial:5 Category:2];
	[mArray addObject:item_sign];
	
	_TableViewDataSource = mArray;
	_TableViewDataSource = [_TableViewDataSource sortedArrayUsingComparator:^(id a, id b)
							{
								if ([a Serial] < [b Serial]) {
									return (NSComparisonResult)NSOrderedAscending;
								}
								else if([a Serial] > [b Serial]) {
									return (NSComparisonResult)NSOrderedDescending;
								}
								return (NSComparisonResult)NSOrderedSame;
							}];
	
}

- (void)showYouMi
{
	[YouMiWall showOffers:YES didShowBlock:nil didDismissBlock:nil];
}

- (void)showDianJoy
{
	if (_DianJoyDelegate == nil) {
		_DianJoyDelegate = [[MRMDianJoyAdWallDetegateInstance alloc] init];
		_DianJoyDelegate.ViewController = self;
	}
	if (_DianJoyView == nil) {
		_DianJoyView = [DianJoyLandingPageView adviewWithDelegate:_DianJoyDelegate];
		_DianJoyDelegate.View = _DianJoyView;
	}
	[DianJoyAdWallParamController setDelegate:_DianJoyDelegate];
	[DianJoyAdWallParamController initSession];
	
	CGRect rect = CGRectZero;
	rect = CGRectMake(0, 20, 320, [UIScreen mainScreen].bounds.size.height - 20);
    [_DianJoyView setFrame:rect];
    [self.view.window addSubview:_DianJoyView];
    
	[_DianJoyView setBackgroundColor:[UIColor colorWithRed:0.988 green:0.988 blue:0.988 alpha:1.0] textColor:[UIColor blackColor] withText:@"正在载入任务⋯⋯" activityViewStyle:UIActivityIndicatorViewStyleGray alpha:1.0];
    
    [_DianJoyView setBlankPage:NO];
	
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 0.6;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.fillMode = kCAFillModeForwards;
    animation.type = kCATransitionMoveIn;
	animation.subtype = kCATransitionFromTop;
    [self.view.layer addAnimation:animation forKey:@"animation"];
}

- (void)showDianRu
{
	if (_DianRuDelegate == nil) {
		_DianRuDelegate = [[MRMDianRuAdWallDetegateInstance alloc] init];
	}
	[DianRuAdWall initAdWallWithDianRuAdWallDelegate:_DianRuDelegate];
	
	[DianRuAdWall showAdWall:self];
}

- (void)showDomob
{
	if (_DomobDelegate == nil) {
		_DomobDelegate = [[MRMDMOfferWallDelegate alloc] init];
	}
	
	if (_DomobWallController == nil) {
		_DomobWallController = [[DMOfferWallViewController alloc] initWithPublisherID:[MRMConfig domobPublisherId] andUserID:[MRMConfig getDeviceId]];
		_DomobWallController.delegate = _DomobDelegate;
	}
	
	[_DomobWallController loadOfferWall];
	
	[_DomobWallController presentOfferWall];
}

- (void)showSign
{
	MRMWebViewController *controller = [[MRMWebViewController alloc] initWithNibName:@"MRMWebViewController" bundle:nil];
	controller.navigationItem.title = @"每日签到";
	controller.UrlString = [NSString stringWithFormat:@"%@/task/checkin?deviceid=%@", Mobile_URL_Prefix, [MRMConfig getDeviceId]];
	NSLog(@"%@", controller.UrlString);
	[self.navigationController pushViewController:controller animated:YES];
	
}

#pragma mark - Table View Data Source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return _TableViewDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *CellIdentifier = @"Cell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil)
	{
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
	}
	
	NSUInteger index = indexPath.row;
	//NSString *key = [[_TableViewDataSource allKeys] objectAtIndex:index];
	TaskItem *task = [_TableViewDataSource objectAtIndex:index];
	
	cell.textLabel.text = task.Name;
	return cell;
}

#pragma mark - Table View Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSUInteger index = indexPath.row;
	TaskItem *task = [_TableViewDataSource objectAtIndex:index];
	NSString *key = task.Key;
	
	if ([key isEqual:@"youmi"]) {
		[self showYouMi];
	}
	else if ([key isEqual:@"dianjoy"]) {
		[self showDianJoy];
	}
	else if ([key isEqual:@"dianru"]) {
		[self showDianRu];
	}
	else if ([key isEqual:@"domob"]) {
		[self showDomob];
	}
	else if ([key isEqual:@"sign"]) {
		[self showSign];
	}
}

@end
